﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Config;

namespace UI
{
    public class UILuckCircleTemplate : MonoBehaviour
    {
        public enum TemplateType
        {
            Hero,
            Equipment,
            Treastrue,
        }
        public TemplateType templateType = TemplateType.Hero;

        #region Icon模版
        public GameObject templateEquip;
        public GameObject templateTreatrue;
        public GameObject templateHeroCenter;
        public GameObject templateHeroOne;
        public GameObject templateHeroTwo;
        public GameObject templateheroThree;
        #endregion
        

        public GameObject centerHero;


        #region 数据ID
        protected string guid;
        public List<UIHeroLuck> lucks;
        public List<int> indices;
        #endregion

        #region cached
        protected List<GameObject> cached = new List<GameObject>();
        #endregion

        #region 排序
        protected List<UIHeroLuck> heroLucks;
        protected List<UIHeroLuck> equipLucks;
        protected List<UIHeroLuck> treaLucks;
        #endregion

        public List<UILuckTemplate> LuckIcons
        {
            get
            {
                var luckIcons = new  List<UILuckTemplate>();
                foreach (var luck in cached)
                {
                     luckIcons.Add(luck.GetComponent<UILuckTemplate>());
                }
                return luckIcons;
            }
        }

        public void Init(List<UIHeroLuck> lucks)
        {
            this.lucks = lucks;
            this.indices = new List<int>();
            this.heroLucks = new List<UIHeroLuck>();
            this.treaLucks = new List<UIHeroLuck>();
            this.equipLucks = new List<UIHeroLuck>();
            int i = 0;
            foreach (var luck in lucks)
            {
                indices.Add(i++);
                if (luck.luckType == Story.LuckType.Hero) heroLucks.Add(luck);
                if (luck.luckType == Story.LuckType.Treasure) treaLucks.Add(luck);
                if (luck.luckType == Story.LuckType.Equipment) equipLucks.Add(luck);
            }

            heroLucks.Sort((lh, rh) => { return rh.Weight - lh.Weight; }); //英雄逆序排列

            SortIndices();
            CreateIcons();
        }

        public void SetCenterIcon(GameObject center)
        {
            center.transform.SetParent(centerHero.transform);
            center.transform.localScale = Vector3.one;
            center.transform.localPosition = Vector3.zero;
            center.transform.localRotation = Quaternion.Euler(Vector3.zero);
        }

        public virtual void Actived(List<bool> light)
        {
        }

        public virtual void Masked(List<bool> masked)
        {
        }

        public virtual Transform GetPlaced(int index)
        {
            return null;
        }
        
        protected virtual void SortIndices()
        {
        }
        
        protected void CreateIcons()
        {
            RemovedOldIcons();
            CreateCenterIcon(lucks[0]);
            int i = 0;
            foreach(var luck in heroLucks) //先安装hero缘分
            {
                CreateIcon(luck, indices[i++]);
            }
            foreach(var luck in equipLucks) //再安装装备缘分
            {
                CreateIcon(luck, indices[i++]);
            }
            foreach(var luck in treaLucks) //最后安装宝物缘分
            {
                CreateIcon(luck, indices[i++]);
            }
        }

        protected GameObject CreateCenterIcon(UIHeroLuck luck)
        {
            GameObject inst = (GameObject)Instantiate(templateHeroCenter);
            inst.transform.SetParent(centerHero.transform);
            inst.transform.localScale = Vector3.one;
            inst.transform.localPosition = Vector3.zero;
            inst.transform.localRotation = Quaternion.Euler(Vector3.zero);
            inst.SetActive(true);
            var script = inst.GetComponent<UILuckTemplate>();
            script.Init(luck);
            cached.Add(inst);
            return inst;
        }

        protected void RemovedOldIcons()
        {
            if(cached.Count > 0)
            {
                foreach(var obj in cached)
                {
                    Destroy(obj);
                }
                cached.Clear();
            }
        }

        protected GameObject CreateIcon(UIHeroLuck luck, int index)
        {
            GameObject temp = null;
            
            if(luck.luckType == Story.LuckType.Hero)
            {
                int i = 0;
                if (!string.IsNullOrEmpty(luck.luckData.hero0)) i++;
                if (!string.IsNullOrEmpty(luck.luckData.hero1)) i++;
                if (!string.IsNullOrEmpty(luck.luckData.hero2)) i++;
                if (!string.IsNullOrEmpty(luck.luckData.hero3)) i++;

                if (i == 1) temp = templateHeroOne;
                if (i == 2) temp = templateHeroTwo;
                if (i == 3) temp = templateheroThree;
                if (i == 4 || i == 0) Debug.LogError("[DEBUG] error luck:" + luck.luckData.id);
            }
            else if(luck.luckType == Story.LuckType.Equipment)
            {
                temp = templateEquip;
            }
            else if(luck.luckType == Story.LuckType.Treasure)
            {
                temp = templateTreatrue;
            }


            GameObject inst = (GameObject)Instantiate(temp);
            inst.transform.SetParent(GetPlaced(index));
            inst.transform.localScale = Vector3.one;
            inst.transform.localPosition = Vector3.zero;
            inst.transform.localRotation = Quaternion.Euler(Vector3.zero);
            inst.SetActive(true);
            cached.Add(inst);

            var script = inst.GetComponent<UILuckTemplate>();
            script.Init(luck);
            return inst;
        }

        public int GetIndex(int value)
        {
            int n = 0;
            foreach(var i in indices)
            {
                if(i==value)
                {
                    return n; 
                }
                n++;
            }

            return n;
        }
    }
}